from rest_framework import serializers
from .models import *


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ["id","name"]

class HostSerializer(serializers.ModelSerializer):
    health_check = serializers.SerializerMethodField()
    groups = GroupSerializer(many=True, required=False,read_only=True)
    group_list = serializers.PrimaryKeyRelatedField(required=False,many=True, read_only=False, queryset=Group.objects.all(),source="groups")

    class Meta:
        model = Host
        fields = ["id","hostname","ip","port","username","password","health_check","group_list","groups"]

    def get_health_check(self,obj):
        return "max_fails=3 fail_timeout=5s"

class HostListSerializer(serializers.ModelSerializer):
    hostid = serializers.CharField(source='id')
    hostip = serializers.CharField(source='ip')
    class Meta:
        model = Host
        fields = ["hostid", "hostname", "hostip", "port", "username", "password"]
        '''
        [{"hostid": "123", "hostname": "192.168.2.150", "hostip": "192.168.2.150", "username": "root",
          "password": "123456", "k1": "v1"}],
        '''

class K8sNodeSerializer(serializers.ModelSerializer):
    model = Host